package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.view.menu.ActionMenuItemView;
import java.util.ArrayList;
import o.ah$j;
import o.au;
import o.bd;
import o.be;
import o.bj;
import o.bo;
import o.br;
import o.bt;
import o.bv;
import o.cm;
import o.cz;
import o.gg;
import o.hd;

public class ActionMenuPresenter extends be implements o.hd.c {
    private int ʻॱ;
    d ʼ;
    private final SparseBooleanArray ʼॱ = new SparseBooleanArray();
    b ʽ;
    private boolean ʽॱ;
    private int ʾ;
    private boolean ʿ;
    private boolean ˈ;
    private e ˉ;
    private View ˊˋ;
    private boolean ˊॱ;
    final i ˋॱ = new i(this);
    private Drawable ˏॱ;
    private boolean ͺ;
    int ॱˊ;
    private boolean ॱˋ;
    private boolean ॱˎ;
    a ॱॱ;
    private int ॱᐝ;
    c ᐝ;
    private int ᐝॱ;

    static class SavedState implements Parcelable {
        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
            public /* synthetic */ Object createFromParcel(Parcel parcel) {
                return ˏ(parcel);
            }

            public /* synthetic */ Object[] newArray(int i) {
                return ˊ(i);
            }

            public SavedState ˏ(Parcel parcel) {
                return new SavedState(parcel);
            }

            public SavedState[] ˊ(int i) {
                return new SavedState[i];
            }
        };
        public int ˋ;

        SavedState() {
        }

        SavedState(Parcel parcel) {
            this.ˋ = parcel.readInt();
        }

        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(this.ˋ);
        }
    }

    class a extends br {
        final /* synthetic */ ActionMenuPresenter ˊ;

        public a(ActionMenuPresenter actionMenuPresenter, Context context, bv bvVar, View view) {
            this.ˊ = actionMenuPresenter;
            super(context, bvVar, view, false, o.ah.c.ͺ);
            if (!((bj) bvVar.getItem()).ʻ()) {
                ˋ(actionMenuPresenter.ʽ == null ? (View) actionMenuPresenter.ʻ : actionMenuPresenter.ʽ);
            }
            ˋ(actionMenuPresenter.ˋॱ);
        }

        public void ˊ() {
            this.ˊ.ॱॱ = null;
            this.ˊ.ॱˊ = 0;
            super.ˊ();
        }
    }

    class b extends AppCompatImageView implements androidx.appcompat.widget.ActionMenuView.b {
        private final float[] ˎ = new float[2];
        final /* synthetic */ ActionMenuPresenter ॱ;

        public b(final ActionMenuPresenter actionMenuPresenter, Context context) {
            this.ॱ = actionMenuPresenter;
            super(context, null, o.ah.c.ˏॱ);
            setClickable(true);
            setFocusable(true);
            setVisibility(0);
            setEnabled(true);
            cz.ˋ(this, getContentDescription());
            setOnTouchListener(new cm(this, this) {
                final /* synthetic */ b ˎ;

                public bt ˎ() {
                    if (this.ˎ.ॱ.ʼ == null) {
                        return null;
                    }
                    return this.ˎ.ॱ.ʼ.ॱ();
                }

                public boolean ˏ() {
                    this.ˎ.ॱ.ˎ();
                    return true;
                }

                public boolean ॱ() {
                    if (this.ˎ.ॱ.ᐝ != null) {
                        return false;
                    }
                    this.ˎ.ॱ.ˊ();
                    return true;
                }
            });
        }

        public boolean performClick() {
            if (!super.performClick()) {
                playSoundEffect(0);
                this.ॱ.ˎ();
            }
            return true;
        }

        public boolean c_() {
            return false;
        }

        public boolean b_() {
            return false;
        }

        protected boolean setFrame(int i, int i2, int i3, int i4) {
            boolean frame = super.setFrame(i, i2, i3, i4);
            Drawable drawable = getDrawable();
            Drawable background = getBackground();
            if (!(drawable == null || background == null)) {
                int width = getWidth();
                int height = getHeight();
                int max = Math.max(width, height) / 2;
                width = (width + (getPaddingLeft() - getPaddingRight())) / 2;
                height = (height + (getPaddingTop() - getPaddingBottom())) / 2;
                gg.ˏ(background, width - max, height - max, width + max, height + max);
            }
            return frame;
        }
    }

    class c implements Runnable {
        private d ˎ;
        final /* synthetic */ ActionMenuPresenter ˏ;

        public c(ActionMenuPresenter actionMenuPresenter, d dVar) {
            this.ˏ = actionMenuPresenter;
            this.ˎ = dVar;
        }

        public void run() {
            if (this.ˏ.ˊ != null) {
                this.ˏ.ˊ.ʻ();
            }
            View view = (View) this.ˏ.ʻ;
            if (!(view == null || view.getWindowToken() == null || !this.ˎ.ˏ())) {
                this.ˏ.ʼ = this.ˎ;
            }
            this.ˏ.ᐝ = null;
        }
    }

    class d extends br {
        final /* synthetic */ ActionMenuPresenter ˎ;

        public d(ActionMenuPresenter actionMenuPresenter, Context context, bd bdVar, View view, boolean z) {
            this.ˎ = actionMenuPresenter;
            super(context, bdVar, view, z, o.ah.c.ͺ);
            ˎ(8388613);
            ˋ(actionMenuPresenter.ˋॱ);
        }

        public void ˊ() {
            if (this.ˎ.ˊ != null) {
                this.ˎ.ˊ.close();
            }
            this.ˎ.ʼ = null;
            super.ˊ();
        }
    }

    class e extends androidx.appcompat.view.menu.ActionMenuItemView.b {
        final /* synthetic */ ActionMenuPresenter ˊ;

        e(ActionMenuPresenter actionMenuPresenter) {
            this.ˊ = actionMenuPresenter;
        }

        public bt ˏ() {
            return this.ˊ.ॱॱ != null ? this.ˊ.ॱॱ.ॱ() : null;
        }
    }

    class i implements o.bn.d {
        final /* synthetic */ ActionMenuPresenter ˊ;

        i(ActionMenuPresenter actionMenuPresenter) {
            this.ˊ = actionMenuPresenter;
        }

        public boolean ˎ(bd bdVar) {
            if (bdVar == null) {
                return false;
            }
            this.ˊ.ॱˊ = ((bv) bdVar).getItem().getItemId();
            o.bn.d ˋ = this.ˊ.ˋ();
            return ˋ != null ? ˋ.ˎ(bdVar) : false;
        }

        public void ॱ(bd bdVar, boolean z) {
            if (bdVar instanceof bv) {
                bdVar.ॱˋ().ˏ(false);
            }
            o.bn.d ˋ = this.ˊ.ˋ();
            if (ˋ != null) {
                ˋ.ॱ(bdVar, z);
            }
        }
    }

    public ActionMenuPresenter(Context context) {
        super(context, ah$j.ˎ, ah$j.ˏ);
    }

    public void ˏ(@NonNull Context context, @Nullable bd bdVar) {
        super.ˏ(context, bdVar);
        Resources resources = context.getResources();
        au ˏ = au.ˏ(context);
        if (!this.ॱˋ) {
            this.ͺ = ˏ.ˏ();
        }
        if (!this.ʽॱ) {
            this.ॱᐝ = ˏ.ˊ();
        }
        if (!this.ॱˎ) {
            this.ᐝॱ = ˏ.ˎ();
        }
        int i = this.ॱᐝ;
        if (this.ͺ) {
            if (this.ʽ == null) {
                this.ʽ = new b(this, this.ॱ);
                if (this.ˊॱ) {
                    this.ʽ.setImageDrawable(this.ˏॱ);
                    this.ˏॱ = null;
                    this.ˊॱ = false;
                }
                int makeMeasureSpec = MeasureSpec.makeMeasureSpec(0, 0);
                this.ʽ.measure(makeMeasureSpec, makeMeasureSpec);
            }
            i -= this.ʽ.getMeasuredWidth();
        } else {
            this.ʽ = null;
        }
        this.ʻॱ = i;
        this.ʾ = (int) (56.0f * resources.getDisplayMetrics().density);
        this.ˊˋ = null;
    }

    public void ॱ(Configuration configuration) {
        if (!this.ॱˎ) {
            this.ᐝॱ = au.ˏ(this.ˏ).ˎ();
        }
        if (this.ˊ != null) {
            this.ˊ.ˎ(true);
        }
    }

    public void ˏ(boolean z) {
        this.ͺ = z;
        this.ॱˋ = true;
    }

    public void ˋ(boolean z) {
        this.ˈ = z;
    }

    public void ˏ(Drawable drawable) {
        if (this.ʽ != null) {
            this.ʽ.setImageDrawable(drawable);
            return;
        }
        this.ˊॱ = true;
        this.ˏॱ = drawable;
    }

    public bo ˏ(ViewGroup viewGroup) {
        bo boVar = this.ʻ;
        bo ˏ = super.ˏ(viewGroup);
        if (boVar != ˏ) {
            ((ActionMenuView) ˏ).setPresenter(this);
        }
        return ˏ;
    }

    public View ˏ(bj bjVar, View view, ViewGroup viewGroup) {
        View actionView = bjVar.getActionView();
        if (actionView == null || bjVar.ˊॱ()) {
            actionView = super.ˏ(bjVar, view, viewGroup);
        }
        actionView.setVisibility(bjVar.isActionViewExpanded() ? 8 : 0);
        ActionMenuView actionMenuView = (ActionMenuView) viewGroup;
        LayoutParams layoutParams = actionView.getLayoutParams();
        if (!actionMenuView.checkLayoutParams(layoutParams)) {
            actionView.setLayoutParams(actionMenuView.ॱ(layoutParams));
        }
        return actionView;
    }

    public void ॱ(bj bjVar, o.bo.b bVar) {
        bVar.ॱ(bjVar, 0);
        ActionMenuItemView actionMenuItemView = (ActionMenuItemView) bVar;
        actionMenuItemView.setItemInvoker((ActionMenuView) this.ʻ);
        if (this.ˉ == null) {
            this.ˉ = new e(this);
        }
        actionMenuItemView.setPopupCallback(this.ˉ);
    }

    public boolean ˏ(int i, bj bjVar) {
        return bjVar.ʻ();
    }

    public void ˊ(boolean z) {
        int i;
        int i2 = 1;
        int i3 = 0;
        super.ˊ(z);
        ((View) this.ʻ).requestLayout();
        if (this.ˊ != null) {
            ArrayList ͺ = this.ˊ.ͺ();
            int size = ͺ.size();
            for (i = 0; i < size; i++) {
                hd ˋ = ((bj) ͺ.get(i)).ˋ();
                if (ˋ != null) {
                    ˋ.ˎ((o.hd.c) this);
                }
            }
        }
        ArrayList ॱˊ = this.ˊ != null ? this.ˊ.ॱˊ() : null;
        if (this.ͺ && ॱˊ != null) {
            i = ॱˊ.size();
            if (i == 1) {
                int i4;
                if (((bj) ॱˊ.get(0)).isActionViewExpanded()) {
                    i4 = 0;
                } else {
                    i4 = 1;
                }
                i3 = i4;
            } else {
                if (i <= 0) {
                    i2 = 0;
                }
                i3 = i2;
            }
        }
        if (i3 != 0) {
            if (this.ʽ == null) {
                this.ʽ = new b(this, this.ॱ);
            }
            ViewGroup viewGroup = (ViewGroup) this.ʽ.getParent();
            if (viewGroup != this.ʻ) {
                if (viewGroup != null) {
                    viewGroup.removeView(this.ʽ);
                }
                ActionMenuView actionMenuView = (ActionMenuView) this.ʻ;
                actionMenuView.addView(this.ʽ, actionMenuView.ˏ());
            }
        } else if (this.ʽ != null && this.ʽ.getParent() == this.ʻ) {
            ((ViewGroup) this.ʻ).removeView(this.ʽ);
        }
        ((ActionMenuView) this.ʻ).setOverflowReserved(this.ͺ);
    }

    public boolean ॱ(ViewGroup viewGroup, int i) {
        if (viewGroup.getChildAt(i) == this.ʽ) {
            return false;
        }
        return super.ॱ(viewGroup, i);
    }

    public boolean ˎ(bv bvVar) {
        if (!bvVar.hasVisibleItems()) {
            return false;
        }
        bv bvVar2 = bvVar;
        while (bvVar2.ᐝॱ() != this.ˊ) {
            bvVar2 = (bv) bvVar2.ᐝॱ();
        }
        View ˋ = ˋ(bvVar2.getItem());
        if (ˋ == null) {
            return false;
        }
        boolean z;
        this.ॱˊ = bvVar.getItem().getItemId();
        int size = bvVar.size();
        for (int i = 0; i < size; i++) {
            MenuItem item = bvVar.getItem(i);
            if (item.isVisible() && item.getIcon() != null) {
                z = true;
                break;
            }
        }
        z = false;
        this.ॱॱ = new a(this, this.ˏ, bvVar, ˋ);
        this.ॱॱ.ˏ(z);
        this.ॱॱ.ˋ();
        super.ˎ(bvVar);
        return true;
    }

    private View ˋ(MenuItem menuItem) {
        ViewGroup viewGroup = (ViewGroup) this.ʻ;
        if (viewGroup == null) {
            return null;
        }
        int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = viewGroup.getChildAt(i);
            if ((childAt instanceof o.bo.b) && ((o.bo.b) childAt).ˏ() == menuItem) {
                return childAt;
            }
        }
        return null;
    }

    public boolean ˎ() {
        if (!this.ͺ || ᐝ() || this.ˊ == null || this.ʻ == null || this.ᐝ != null || this.ˊ.ॱˊ().isEmpty()) {
            return false;
        }
        this.ᐝ = new c(this, new d(this, this.ˏ, this.ˊ, this.ʽ, true));
        ((View) this.ʻ).post(this.ᐝ);
        super.ˎ(null);
        return true;
    }

    public boolean ˊ() {
        if (this.ᐝ == null || this.ʻ == null) {
            br brVar = this.ʼ;
            if (brVar == null) {
                return false;
            }
            brVar.ˎ();
            return true;
        }
        ((View) this.ʻ).removeCallbacks(this.ᐝ);
        this.ᐝ = null;
        return true;
    }

    public boolean ʻ() {
        return ˊ() | ॱॱ();
    }

    public boolean ॱॱ() {
        if (this.ॱॱ == null) {
            return false;
        }
        this.ॱॱ.ˎ();
        return true;
    }

    public boolean ᐝ() {
        return this.ʼ != null && this.ʼ.ᐝ();
    }

    public boolean ʼ() {
        return this.ᐝ != null || ᐝ();
    }

    public boolean ˏ() {
        int size;
        ArrayList arrayList;
        int i;
        if (this.ˊ != null) {
            ArrayList ʽ = this.ˊ.ʽ();
            size = ʽ.size();
            arrayList = ʽ;
        } else {
            size = 0;
            arrayList = null;
        }
        int i2 = this.ᐝॱ;
        int i3 = this.ʻॱ;
        int makeMeasureSpec = MeasureSpec.makeMeasureSpec(0, 0);
        ViewGroup viewGroup = (ViewGroup) this.ʻ;
        int i4 = 0;
        int i5 = 0;
        Object obj = null;
        int i6 = 0;
        while (i6 < size) {
            bj bjVar = (bj) arrayList.get(i6);
            if (bjVar.ॱˊ()) {
                i4++;
            } else if (bjVar.ˏॱ()) {
                i5++;
            } else {
                obj = 1;
            }
            if (this.ˈ && bjVar.isActionViewExpanded()) {
                i = 0;
            } else {
                i = i2;
            }
            i6++;
            i2 = i;
        }
        if (this.ͺ && (r4 != null || i4 + i5 > i2)) {
            i2--;
        }
        i6 = i2 - i4;
        SparseBooleanArray sparseBooleanArray = this.ʼॱ;
        sparseBooleanArray.clear();
        i = 0;
        if (this.ʿ) {
            i = i3 / this.ʾ;
            i5 = ((i3 % this.ʾ) / i) + this.ʾ;
        } else {
            i5 = 0;
        }
        int i7 = 0;
        i2 = 0;
        int i8 = i;
        while (i7 < size) {
            bjVar = (bj) arrayList.get(i7);
            int i9;
            if (bjVar.ॱˊ()) {
                View ˏ = ˏ(bjVar, this.ˊˋ, viewGroup);
                if (this.ˊˋ == null) {
                    this.ˊˋ = ˏ;
                }
                if (this.ʿ) {
                    i8 -= ActionMenuView.ॱ(ˏ, i5, i8, makeMeasureSpec, 0);
                } else {
                    ˏ.measure(makeMeasureSpec, makeMeasureSpec);
                }
                i4 = ˏ.getMeasuredWidth();
                i9 = i3 - i4;
                if (i2 != 0) {
                    i4 = i2;
                }
                i2 = bjVar.getGroupId();
                if (i2 != 0) {
                    sparseBooleanArray.put(i2, true);
                }
                bjVar.ˊ(true);
                i = i9;
                i2 = i6;
            } else if (bjVar.ˏॱ()) {
                boolean z;
                int groupId = bjVar.getGroupId();
                boolean z2 = sparseBooleanArray.get(groupId);
                boolean z3 = (i6 > 0 || z2) && i3 > 0 && (!this.ʿ || i8 > 0);
                if (z3) {
                    View ˏ2 = ˏ(bjVar, this.ˊˋ, viewGroup);
                    if (this.ˊˋ == null) {
                        this.ˊˋ = ˏ2;
                    }
                    boolean z4;
                    if (this.ʿ) {
                        int ॱ = ActionMenuView.ॱ(ˏ2, i5, i8, makeMeasureSpec, 0);
                        i9 = i8 - ॱ;
                        if (ॱ == 0) {
                            i8 = 0;
                        } else {
                            z4 = z3;
                        }
                        i4 = i9;
                    } else {
                        ˏ2.measure(makeMeasureSpec, makeMeasureSpec);
                        boolean z5 = z3;
                        i4 = i8;
                        z4 = z5;
                    }
                    i9 = ˏ2.getMeasuredWidth();
                    i3 -= i9;
                    if (i2 == 0) {
                        i2 = i9;
                    }
                    if (this.ʿ) {
                        z = i8 & (i3 >= 0 ? 1 : 0);
                        i9 = i2;
                        i2 = i4;
                    } else {
                        z = i8 & (i3 + i2 > 0 ? 1 : 0);
                        i9 = i2;
                        i2 = i4;
                    }
                } else {
                    z = z3;
                    i9 = i2;
                    i2 = i8;
                }
                if (z && groupId != 0) {
                    sparseBooleanArray.put(groupId, true);
                    i8 = i6;
                } else if (z2) {
                    sparseBooleanArray.put(groupId, false);
                    i4 = i6;
                    for (i6 = 0; i6 < i7; i6++) {
                        bj bjVar2 = (bj) arrayList.get(i6);
                        if (bjVar2.getGroupId() == groupId) {
                            if (bjVar2.ʻ()) {
                                i4++;
                            }
                            bjVar2.ˊ(false);
                        }
                    }
                    i8 = i4;
                } else {
                    i8 = i6;
                }
                if (z) {
                    i8--;
                }
                bjVar.ˊ(z);
                i4 = i9;
                i = i3;
                int i10 = i2;
                i2 = i8;
                i8 = i10;
            } else {
                bjVar.ˊ(false);
                i4 = i2;
                i = i3;
                i2 = i6;
            }
            i7++;
            i3 = i;
            i6 = i2;
            i2 = i4;
        }
        return true;
    }

    public void ˏ(bd bdVar, boolean z) {
        ʻ();
        super.ˏ(bdVar, z);
    }

    public Parcelable ʽ() {
        Parcelable savedState = new SavedState();
        savedState.ˋ = this.ॱˊ;
        return savedState;
    }

    public void ˏ(Parcelable parcelable) {
        if (parcelable instanceof SavedState) {
            SavedState savedState = (SavedState) parcelable;
            if (savedState.ˋ > 0) {
                MenuItem findItem = this.ˊ.findItem(savedState.ˋ);
                if (findItem != null) {
                    ˎ((bv) findItem.getSubMenu());
                }
            }
        }
    }

    public void ˎ(boolean z) {
        if (z) {
            super.ˎ(null);
        } else if (this.ˊ != null) {
            this.ˊ.ˏ(false);
        }
    }

    public void ˋ(ActionMenuView actionMenuView) {
        this.ʻ = actionMenuView;
        actionMenuView.ˎ(this.ˊ);
    }
}
